package com.hnevc.contentproviderdemo;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ModifyActivity extends AppCompatActivity {
    public static final int RESULT_CODE = 110;
    int _id;
    private News mNews;
    private TextView mTvTitle;
    private TextView mTvAuthor;
    private TextView mTvContent;
    private Button mBtnSave,mBtnCancel;
    private Uri mUri;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify);

        _id = getIntent().getIntExtra("_id",0);
        mNews = getNewsById(_id);
        initView();
        initEvent();
    }

    private void initEvent() {
        mBtnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mNews.setTitle(mTvTitle.getText().toString());
                mNews.setAuthor(mTvAuthor.getText().toString());
                mNews.setContent(mTvContent.getText().toString());

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                Date time = Calendar.getInstance().getTime();
                String datestr = sdf.format(time);
                mNews.setPublishDate(datestr);

                ContentResolver resolver = getContentResolver();
                ContentValues values = new ContentValues();
                values.put("title",mNews.getTitle());
                values.put("author",mNews.getAuthor());
                values.put("content",mNews.getContent());
                values.put("publishDate",mNews.getPublishDate());
                int num = resolver.update(mUri,values,"_id=?",new String[]{mNews.get_id()+""});
                if(num>0){
                    //修改成功,更新界面
                    Intent intent = new Intent();
                    intent.putExtra("_id",mNews.get_id());
                    intent.putExtra("title",mNews.getTitle());
                    intent.putExtra("author",mNews.getAuthor());
                    intent.putExtra("content",mNews.getContent());
                    intent.putExtra("publishDate",mNews.getPublishDate());
                    setResult(RESULT_CODE,intent);
                    ModifyActivity.this.finish();
                }
            }
        });
    }

    private void initView() {
        mTvTitle = (TextView) findViewById(R.id.id_tv_title);
        mTvAuthor = (TextView) findViewById(R.id.id_tv_author);
        mTvContent = (TextView) findViewById(R.id.id_tv_content);
        if(mNews!=null) {
            mTvTitle.setText(mNews.getTitle());
            mTvAuthor.setText(mNews.getAuthor());
            mTvContent.setText(mNews.getContent());
        }
        mBtnSave  = (Button) findViewById(R.id.id_btn_save);


    }

    private News getNewsById(int id) {
        mUri = Uri.parse("content://com.hnevc.contentproviderdemo/news/"+id);
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(mUri,null,null,null,null);
        News news = null;
        if(cursor.moveToFirst()){
            news = new News();
            news.set_id(id);
            news.setTitle(cursor.getString(cursor.getColumnIndex("title")));
            news.setAuthor(cursor.getString(cursor.getColumnIndex("author")));
            news.setContent(cursor.getString(cursor.getColumnIndex("content")));
            news.setPublishDate(cursor.getString(cursor.getColumnIndex("publishDate")));
        }
        return news;
    }
}
